package hhm.android.library.utils

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.preferencesKey
import androidx.datastore.preferences.createDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

object DataStoreUtils {

    val TOKEN = "TOKEN"
    val SUM_TIME = "SUM_TIME"
    val OLD_TIME = "OLD_TIME"
    val ALIAS = "userAlias"

    fun init(context: Context) {
        mContext = context
    }

    private var dataStore: DataStore<Preferences>? = null
    private const val dataStoreName: String = "seven_bubble"
    private lateinit var mContext: Context

    /**
     * 写入String数据
     */
    suspend fun writeStringToDataStore(key: String, msg: String) {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<String>(key)
        dataStore!!.edit { user ->
            user[data] = msg
        }
    }

    /**
     * 写入Int数据
     */
    suspend fun writeIntToDataStore(key: String, num: Int) {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Int>(key)
        dataStore!!.edit { user ->
            user[data] = num
        }
    }

    /**
     * 写入Long数据
     */
    suspend fun writeLongToDataStore(key: String, num: Long) {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Long>(key)
        dataStore!!.edit { user ->
            user[data] = num
        }
    }

    /**
     * 写入Float数据
     */
    suspend fun writeFloatToDataStore(key: String, num: Float) {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Float>(key)
        dataStore!!.edit { user ->
            user[data] = num
        }
    }

    /**
     * 写入Boolean数据
     */
    suspend fun writeBooleanToDataStore(key: String, b: Boolean) {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Boolean>(key)
        dataStore!!.edit { user ->
            user[data] = b
        }
    }

    /**
     * 写入Double数据
     */
    suspend fun writeDoubleToDataStore(key: String, num: Double) {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Double>(key)
        dataStore!!.edit { user ->
            user[data] = num
        }
    }

    /**
     * 读取String数据
     */
    fun readStringFromDataStore(key: String): Flow<String> {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<String>(key)
        return dataStore!!.data.map { preferences: Preferences -> preferences[data] ?: "" }
    }

    /**
     * 读取Int数据
     */
    fun readIntFromDataStore(key: String): Flow<Int> {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Int>(key)
        return dataStore!!.data.map { preferences: Preferences -> preferences[data] ?: 0 }
    }

    /**
     * 读取Long数据
     */
    fun readLongFromDataStore(key: String): Flow<Long> {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Long>(key)
        return dataStore!!.data.map { preferences: Preferences -> preferences[data] ?: 0L }
    }

    /**
     * 读取Float数据
     */
    fun readFloatFromDataStore(key: String): Flow<Float> {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Float>(key)
        return dataStore!!.data.map { preferences: Preferences -> preferences[data] ?: 0F }
    }

    /**
     * 读取Boolean数据
     */
    fun readBooleanFromDataStore(key: String): Flow<Boolean> {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Boolean>(key)
        return dataStore!!.data.map { preferences: Preferences -> preferences[data] ?: false }
    }

    /**
     * 读取Double数据
     */
    fun readDoubleFromDataStore(key: String): Flow<Double> {
        if (dataStore == null) {
            dataStore = mContext.createDataStore(dataStoreName)
        }

        val data = preferencesKey<Double>(key)
        return dataStore!!.data.map { preferences: Preferences -> preferences[data] ?: 0.0 }
    }

}